package com.shen.jcbf.thread;


import java.util.HashMap;
import java.util.Hashtable;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 怜笑
 * @company 公司
 * @create 2019-08-07 10:35
 */
public class TestVolatile {

//    public volatile int inc = 0;

    public AtomicInteger inc = new AtomicInteger(0);


    public void increase() {
        inc.incrementAndGet();
    }

    public static void main(String[] args) {
        final TestVolatile test = new TestVolatile();
        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        test.increase();
                    }
                }

                ;
            }.start();
        }

        //保证前面的线程都执行完
        while (Thread.activeCount() > 1) {
            Thread.yield();
        }
        System.out.println(test.inc);

        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        concurrentHashMap.put("aa", "");

        HashMap hashMap = new HashMap();
        hashMap.put("","");

        Hashtable hashtable = new Hashtable();
        hashtable.put("", "");
    }


    // *****************
    public void test002() throws InterruptedException {

        final HashMap<String, String> map = new HashMap<String, String>(2);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            map.put(UUID.randomUUID().toString(), "");
                        }
                    }).start();
                }
            }
        });
        t.start();
        t.join();

    }
}
